package com.springcloud.tools.token;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author fuc
 * @create 2020-08-19
 */
@Service
public class RedisService {

  @Autowired RedisTemplate redisTemplate;

  public Boolean setEx(String key, Object value, Long expireTime) {
    try {
      ValueOperations ops = redisTemplate.opsForValue();
      ops.set(key, value);
      redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
      return true;
    } catch (Exception e) {
      e.printStackTrace();
    }
    return false;
  }

  public Boolean exists(String key) {
    return redisTemplate.hasKey(key);
  }

  public Boolean remove(String key) {
    if (exists(key)) {
      return redisTemplate.delete(key);
    }
    return false;
  }
}
